package co.edu.unal.bioingenium.kbmed.web.beans;

import co.edu.unal.bioingenium.kbmed.knowledge.visualization.VisualizationService;
import co.edu.unal.bioingenium.kbmed.retrieval.data.RetrievalData;
import co.edu.unal.bioingenium.kbmed.retrieval.knowledge.process.RetrieveService;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Luis A Riveros
 */
@ManagedBean(name = "retrieveConcepts")
@SessionScoped
public class RetrieveConceptsBean implements Serializable {

    private RetrieveService retrieveService;
    private VisualizationService visualizationService;
    private String queryStr;
    private String subHierarchyJson;
    private long retrievalTime;
    private RetrievalData retrievalData;

    /** Creates a new instance of RetrieveConceptsBean */
    public RetrieveConceptsBean() {
        retrieveService = RetrieveService.getInstance();
        visualizationService = VisualizationService.getInstance();
        init();
    }

    /**
     * 
     */
    public final void init() {
        retrievalData = new RetrievalData();
        queryStr = "";
        subHierarchyJson = "";
        retrievalTime = 0;
    }

    /**
     * 
     * @return
     */
    public String getQueryStr() {
        return queryStr;
    }

    /**
     * 
     * @param queryStr
     */
    public void setQueryStr(String queryStr) {
        this.queryStr = queryStr;
    }

    /**
     * 
     * @return
     */
    public RetrievalData getRetrievalData() {
        return retrievalData;
    }

    /**
     * 
     * @param retrievalData
     */
    public void setRetrievalData(RetrievalData retrievalData) {
        this.retrievalData = retrievalData;
    }

    /**
     * 
     * @return
     */
    public String getSubHierarchyJson() {
        return subHierarchyJson;
    }

    /**
     * 
     * @param subHierarchyJson
     */
    public void setSubHierarchyJson(String subHierarchyJson) {
        this.subHierarchyJson = subHierarchyJson;
    }

    /**
     * 
     * @return
     */
    public long getRetrievalTime() {
        return retrievalTime;
    }

    /**
     * 
     * @param retrievalTime
     */
    public void setRetrievalTime(long retrievalTime) {
        this.retrievalTime = retrievalTime;
    }

    /**
     * 
     * @return
     */
    public int getResultsSize() {
        return retrievalData.getResults().size();
    }

    /**
     * 
     */
    public void retrieve() {
        Logger.getLogger(RetrieveConceptsBean.class.getName()).log(Level.INFO, "Run query for input: {0}", queryStr);
        long start = System.currentTimeMillis();
        int splitIdx;
        if ((splitIdx = queryStr.indexOf("|")) != -1) {
            String tempQueryStr = queryStr.substring(splitIdx);
            StringTokenizer tokenizer = new StringTokenizer(tempQueryStr, "+:");
            Set<String> addedDescriptorIds = new HashSet<String>();
            queryStr = queryStr.substring(0, splitIdx);
            while (tokenizer.hasMoreTokens()) {
                tokenizer.nextToken();
                addedDescriptorIds.add(tokenizer.nextToken());
                queryStr += tokenizer.nextToken();
            }
            retrieveService.retrieveByConcepts(retrievalData, addedDescriptorIds);
        } else {
            if (retrievalData.hasPreviusData()) {
                retrievalData.clearPreviusData();
            }
            retrieveService.retrieveByConcepts(retrievalData, queryStr);
        }
        retrievalTime = System.currentTimeMillis() - start;
        setSubHierarchyJson(visualizationService.getSubHierarchyJson(retrievalData, retrieveService.getCollectionData().getConceptCorpus()));
    }
}
